package com.euq.controller;

import com.euq.dto.Ret;
import com.euq.entity.User;
import com.euq.log.BusinessLog;
import com.euq.log.Log;
import com.euq.mq.LoginLogProducer;
import com.euq.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account")
public class AccountController {
    private static final Logger log = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private LoginLogProducer loginLogProducer;
    @PostMapping("/login")
    @Log
    public Object login(@RequestParam("account")String account, @RequestParam("password")String password) throws Exception {
        log.info("用户{}尝试登录", account);
        Object result= userService.login(account, password);
        log.info("用户{}登录成功，返回结果{}",account,result);
        loginLogProducer.sendLoginLog("login-topic", "用户登录成功: " + account);
        return Ret.success(result);
    }
    @PostMapping("/register")
    @Log
    public Object register(@RequestBody User user) throws Exception {
        log.info("注册新账号{}",user.getAccount());
        log.info("新用户注册成功{}",user);
        return userService.register(user);
    }

    @PostMapping("/loginV2")
    @Log
    public Object loginV2(@RequestParam("account")String account, @RequestParam("password")String password) throws Exception {

        log.info("AccountController.loginV2 account:{} passwsord:{}", account, password);
        String result = userService.findByAccountV2(account, password);
        log.info("AccountController.loginV2 result:{}", result);
        return Ret.success(result);
    }

    @PostMapping("/updatePwd")
    @BusinessLog(logName = "修改密码",logType = "业务日志")
    public Object updatePwd(String oldPwd, String newPwd, String rePwd) throws Exception {
        return userService.updatePwd(oldPwd, newPwd, rePwd);
    }
}
